<template>
	<view>
		<view class="p-2 border-bottom flex align-center bg-white">
			<view class="font-md text-secondary mr-1 flex-shrink">收货人:</view>
			<input class="pl-1 font-md flex-1" type="text" v-model="address.receiverName" />
		</view>
		<view class="p-2 border-bottom flex align-center bg-white">
			<view class="font-md text-secondary mr-1 flex-shrink">手机号:</view>
			<input class="pl-1 font-md flex-1" type="text" v-model="address.receiverMobile" />
		</view>
		
		<view class="p-2 border-bottom flex align-center bg-white mt-2">
			<view class="font-md text-secondary mr-1 flex-shrink">所在地区:</view>
			<uni-data-picker  v-model="selectedAddress" class="flex-1" :border="false" :localdata="addressList" popup-title="请选择班级" @change="addressOnchange" @nodeclick="onnodeclick"></uni-data-picker>
		</view>
		<view class="p-2 border-bottom flex align-center bg-white">
			<view class="font-md text-secondary mr-1 flex-shrink">详细地址:</view>
			<input class="pl-1 font-md flex-1" type="text" v-model="address.detailAddress" />
		</view>
		
		<view class="p-2 border-bottom flex align-center bg-white mt-2">
			<view class="font-md text-secondary mr-1 flex-shrink">设为默认地址:</view>
			<switch class="ml-auto" color="#FC4353" :checked="address.isDefault == 1" @change="defaultAddressChange" />
		</view>
		
		<view class="p-3" @click="saveAddress">
			<view class="text-center w-100 main-bg-color font-md rounded py-2 text-white">
				保存
			</view>
		</view>
		
		
	</view>
</template>

<script setup>
	import { ref, reactive } from "vue";
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import { getAllAddress, addDeliveryAddress, editDeliveryAddress } from "@/api/address/index.js"
	
	const selectedAddress = ref("")
	
	 const address = ref({
	  provinceCode: "",
	  provinceName: "",
	  cityCode: "",
	  cityName: "",
	  districtCode: "",
	  districtName: "",
	  detailAddress: "",
	  receiverName: "",
	  receiverMobile: "",
	  isDefault: 0
	})
	
	onLoad(async(e)=>{
		console.log("获取参数=========",e);
		if(e.data){
			// 修改
			let data = JSON.stringify(JSON.parse(e.data), ["id","provinceCode","provinceName","cityCode","cityName","districtCode","districtName",
			"detailAddress","receiverName","receiverMobile","receiverMobile","isDefault"])
			address.value = JSON.parse(data)
			
			selectedAddress.value = address.value.districtCode
			setTimeout(()=>{
				uni.setNavigationBarTitle({
					title:"编辑收货地址"
				})
			},100)
		}else{
			setTimeout(()=>{
				uni.setNavigationBarTitle({
					title:"添加收货地址"
				})
			},100)
		}
		let res = await getAllAddress()
		console.log(res);
		addressList.value = res.data
	})
	
	const addressList = ref([])
	
	const defaultAddressChange = () => {
		if(address.value.isDefault == 1){
			address.value.isDefault = 0
		}else{
			address.value.isDefault = 1
		}
	}
	
	const addressOnchange = (e) =>{
		console.log("addressOnchange",e);
		let arr = e.detail.value
		address.value.provinceCode = arr[0].value
		address.value.provinceName = arr[0].text
		address.value.cityCode = arr[1].value
		address.value.cityName = arr[1].text
		address.value.districtCode = arr[2].value
		address.value.districtName = arr[2].text
	}
	
	const saveAddress = async ()=>{
		console.log("保存数据",address.value);
		if(address.value.id){
			let res = await editDeliveryAddress(address.value);
		}else{
			let res = await addDeliveryAddress(address.value);
		}
		uni.showToast({
			title:"保存成功",
			mask: true
		})
		uni.navigateBack()
	}
	
	
	const onnodeclick = (e) => {
		console.log("onnodeclick",e);
	}
</script>

<style scoped lang="scss">
	
</style>
